package com.jtpay.ysfproductionprocess.ysf;

import android.text.TextUtils;
import android.util.Log;
import com.jtpay.ysfproductionprocess.BuildConfig;
import com.jtpay.ysfproductionprocess.Constant.YsfConstant;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;

import static com.jtpay.ysfproductionprocess.utils.StringUtil.*;

/**
 * 这个类是为了通过注入启动云闪付
 */
public class YsfLaunch {
    private static final String TAG = "frida";

    private void log(String content) {
        if (BuildConfig.DEBUG) {
            Log.d(TAG, "msg:" + content);
        }
    }

    private void send(String s, Socket socket) {
        byte[] b = strToByteArray(s);
        try {
            OutputStream outputStream = socket.getOutputStream();
            outputStream.write(b, 0, b.length);
            outputStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void send(byte[] b, Socket socket) {
        try {
            OutputStream outputStream = socket.getOutputStream();
            outputStream.write(b, 0, b.length);
            outputStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void startYsfActivity() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    int mod = 0;
                    int step = 0;
                    String pid = "";
                    String endString = "";
                    String agentSession = "";
                    byte[] bytes = new byte[1024 * 4];
                    Socket socket = new Socket("127.0.0.1", 27042);
                    String auth0 = "\0AUTH\r\n";
                    send(auth0, socket);
                    int size = socket.getInputStream().read(bytes);
                    while (true) {
                        if (size > 0) {
                            String ret = byteArrayToStr(bytes);
                            log("msg:" + ret);
                            String result = bytesToHexString(bytes);
                            if (TextUtils.isEmpty(result)) {
                                continue;
                            }
                            String hex = result.toUpperCase();
                            log("hex:" + hex);
                            if (hex.length() > 68) {
                                if ((hex.length() - hex.indexOf("6C020101")) == 68) {
                                    endString = hex.substring(hex.length() - 68, hex.length());
                                } else if ((hex.length() - hex.indexOf("6C020101")) == 67) {
                                    endString = hex.substring(hex.length() - 67, hex.length());
                                }
                            }
                            if (ret.startsWith("REJECTED EXTERNAL ANONYMOUS DBUS_COOKIE_SHA1")) {
                                String auth1 = "AUTH ANONYMOUS 474442757320302e31\r\n";
                                send(auth1, socket);
                            } else if (ret.startsWith("OK ")) {
                                send("BEGIN\r\n", socket);
                                send(hexStringToBytes(YsfConstant.m_data1), socket);
                            } else if (hex.contains("6C0201010800000001000000180000000801670005617B73767D0000000000000501750001")) {
                                send(hexStringToBytes(YsfConstant.m_data2), socket);
                            } else if (hex.length() == 74) {
                                if (mod == 0) {
                                    send(hexStringToBytes(String.format(YsfConstant.data, agentSession)), socket);
                                } else if (mod == 1) {
                                    send(hexStringToBytes(String.format(YsfConstant.data2, agentSession)), socket);
                                }
                            } else if (hex.length() == 82 && step == 1) {
                                send(hexStringToBytes(String.format(YsfConstant.m_data_loadscript, agentSession)), socket);
                            } else if (hex.length() == 58) {
                                String resume = YsfConstant.m_data_resume + pid + "0000";
                                send(hexStringToBytes(resume), socket);
                            } else if (hex.length() == 68 || endString.length() == 68) {
                                if (endString.length() == 68) {
                                    pid = endString.substring(64, 68);
                                } else {
                                    pid = hex.substring(64, 68);
                                }
                                log("------------------------------------------pid:" + pid);
                                String attache = YsfConstant.m_data3 + pid + "0000";
                                send(hexStringToBytes(attache), socket);
                            } else if (hex.length() == 67) {
                                hex = hex + "0";
                                pid = hex.substring(64, 68);
                                log("------------------------------------------pid:" + pid);
                                String attache = YsfConstant.m_data3 + pid + "0000";
                                send(hexStringToBytes(attache), socket);
                            } else if (hex.length() == 82 && step == 0) {
                                agentSession = hex.substring(80, 82);
                                int aSession = Integer.parseInt(agentSession, 16);
                                log("------------------------------------------agentSession:" + agentSession + ";" + aSession);
                                if (aSession > 9) {
                                    mod = 1;
                                }
                                agentSession = ToHex(String.valueOf(aSession), false);
                                agentSession = agentSession.replace("20", "00");
                                send(hexStringToBytes(String.format(YsfConstant.m_data_getAll, agentSession)), socket);
                                step = 1;
                            }
                        }
                        size = 0;
                        endString = "";
                        while (size == 0) {
                            size = socket.getInputStream().available();
                        }
                        bytes = new byte[1024 * 4];
                        size = socket.getInputStream().read(bytes);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
